import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:jd_app/model/homePage_model.dart';
import 'package:jd_app/provider/home_provider.dart';
import 'package:provider/provider.dart';
import 'package:card_swiper/card_swiper.dart';

class HomePage extends StatefulWidget {
  const HomePage({super.key});

  @override
  State<HomePage> createState() => _HomePageState();
}

class _HomePageState extends State<HomePage> {
  @override
  Widget build(BuildContext context) {
    return Container(
      child: ChangeNotifierProvider(
        create: (context) {
          print('create HomeProvider');
          HomeProvider provider = HomeProvider();
          provider.loadHomeData();
          return provider;
        },
        child: Consumer(
          builder: (context, HomeProvider provider, child) {
             // 展示loading
            if (provider.isLoading) {
              return Center(
                  child: CupertinoActivityIndicator(),
              );
            };
            // 如果出错展示错误
            if (provider.isError) {
              return Center(
                child: Column(
                  children: [
                    Padding(
                      padding: EdgeInsets.only(top: 100, left: 10, right: 10),
                      child: Text(provider.errMsg),
                    ),
                    OutlinedButton(
                      onPressed: () {
                        provider.loadHomeData();
                      },
                      child: Text("重新加载"),
                      )
                  ]
                ),
              );
            }
            return Scaffold(
              appBar: AppBar(
                title: const Text(
                  "首页",
                  style: TextStyle(
                    color: Colors.white,
                    fontSize: 16,
                    fontWeight: FontWeight.bold
                  ),
                ),
                centerTitle: true,
                backgroundColor: Colors.red[600],
              ),
              body: ListView(
                children: [
                  // 首页轮播图
                  homeSwiper(provider),
                  // 分类图标
                  // 秒杀模块
                  // 广告模块
                ],
              ),
            );
          }
        )
      )
    );
  }
}



// 首页轮播图
Widget homeSwiper(HomeProvider provider) {
  var data = provider.data;
  // var swipers = data.swipers;
  List<swiperImgObj> swipers = [
        swiperImgObj(image: "https://luckycola.com.cn/public/imgs/luckycola_Imghub_forever_aGQClIPU16848264594546698.jpg"),
        swiperImgObj(image: "https://luckycola.com.cn/public/imgs/luckycola_Imghub_forever_i4jIMzE316857298728959661.jpeg"),
        swiperImgObj(image: "https://luckycola.com.cn/public/imgs/luckycola_Imghub_forever_ITWi4BOo16848147825389630.jpg")
      ];
  
  return Container(
    height: 200,
    child: AspectRatio(
      aspectRatio: 2 / 1,
      child: Swiper(
        itemCount: swipers.length,
        autoplay: true,
        pagination: new SwiperPagination(),
        itemBuilder: (BuildContext context, int index) {
          var image = swipers[index] .image;
          return Image.network(image, fit: BoxFit.fill);
        },
      ),
    ),
  );
}